define(['jquery', 'bootstrap', 'backend', 'table', 'form',], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'rooms/index',
                    add_url: 'rooms/add',
                    edit_url: 'rooms/edit',
                    del_url: 'rooms/del',
                    multi_url: 'rooms/multi',
                    setprice_url: 'rooms/setprice',
                    dragsort_url: 'ajax/weigh',
                    table: 'category',
                }
            });

            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate: false},
                        {field: 'h_title', title: __('h_title'), align: 'left'},
                        {field: 'h_img', title: __('Image'), operate: false, formatter: Table.api.formatter.image},//height:'50px'
                        {field: 'h_housebrand', title: __('h_housebrand'), align: 'left'},
                        {field: 'h_price', title: __('h_price'), align: 'left'},
                        {field: 'h_allprice', title: __('h_allprice'), align: 'left'},
                        {field: 'h_storey', title: __('h_storey'), align: 'left'},
                        {field: 'choice.h_unit', title: __('hunit'), align: 'left'},
                        {field: 'choice.h_title', title: __('htitle'), align: 'left'},
                        {field: 'h_status', title: __('Status'), operate: false, formatter: Table.api.formatter.status},
                        {
                            field: 'h_hot',
                            title: __('h_hot'),
                            align: 'center',
                            formatter: Table.api.formatter.toggle
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}

                    ]
                ],
                commonSearch: false,

            };

            // 初始化表格
            table.bootstrapTable(tableOptions);

            // 为表格绑定事件
            Table.api.bindevent(table);

            //修改添加是弹窗大小
            $(".btn-add").data("area", ["80%","80%"]); //自定义弹窗大小
            //修改编辑弹窗大小
            table.on('post-body.bs.table',function () {
                $(".btn-editone").data("area", ["80%","80%"]); //自定义弹窗大小
            });

            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#', '');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    // params.filter = JSON.stringify({type: typeStr});
                    params.type = typeStr;

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });

            //必须默认触发shown.bs.tab事件
            // $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");

        },

        add: function () {
            Controller.api.bindevent();
            setTimeout(function () {
                $("#c-type").trigger("change");
            }, 100);
        },
        setprice: function () {//没新增一个弹窗页面 这里都要配置，否则 执行成功后 无法关闭弹窗
            Controller.api.bindevent();
            setTimeout(function () {
                $("#c-type").trigger("change");
            }, 100);
        },

        edit: function () {

            //渲染页面 选中数据
            //选中入住须知
             var hreads = $("#hreads").val();
             var strsRead= new Array(); //定义一数组
              strsRead = hreads.split(",");
              for (i=0;i<strsRead.length ;i++ )
              {
                  $("#hreadsval input[value="+ strsRead[i] +"]").attr("checked","checked");

              }

            //选中卫浴设施
            var hservice2val = $("#h_service2").val();
            var hservice2arr= new Array(); //定义一数组
            hservice2arr = hservice2val.split(",");
            for (i=0;i<hservice2arr.length ;i++ )
            {
                $("#h_service2val input[value="+ hservice2arr[i] +"]").attr("checked","checked");

            }

            //选中基础设施
            var hserviceval = $("#h_service").val();
            var hservicearr= new Array(); //定义一数组
            hservicearr = hserviceval.split(",");
            for (i=0;i<hservicearr.length ;i++ )
            {
                $("#h_serviceval input[value="+ hservicearr[i] +"]").attr("checked","checked");

            }

            //选中房间设施
            var h_servsval = $("#h_servs").val();
            var h_servsvalarr= new Array(); //定义一数组
            h_servsvalarr = h_servsval.split(",");
            for (i=0;i<h_servsvalarr.length ;i++ )
            {
                $("#h_servsval input[value="+ h_servsvalarr[i] +"]").attr("checked","checked");

            }



            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).on("change", "#c-type", function () {
                    $("#c-pid option[data-type='all']").prop("selected", true);
                    $("#c-pid option").removeClass("hide");
                    $("#c-pid option[data-type!='" + $(this).val() + "'][data-type!='all']").addClass("hide");
                    $("#c-pid").data("selectpicker") && $("#c-pid").selectpicker("refresh");
                });
                Form.api.bindevent($("form[role=form]"));
            },

        }
    };
    return Controller;
});
